package com.example.demo.context;


public class ContextHolder {
	 /**
     * 用户相关上下文
     */
    private static ThreadLocal<UserContext> userContext = new ThreadLocal<UserContext>() {
        @Override
        protected UserContext initialValue() {
            UserContext userContext = new UserContext();
            return userContext;
        }
    };

    /**
     * 运行环境相关上下文
     */
    private static ThreadLocal<EnvironContext> environContext = new ThreadLocal<EnvironContext>() {
        @Override
        protected EnvironContext initialValue() {
            EnvironContext environContext = new EnvironContext();
            return environContext;
        }
    };

    public static UserContext getUserContext() {
        return userContext.get();
    }

    public static void clearUserContext() {
        getUserContext().clear();
        userContext.remove();
    }

    public static EnvironContext getEnvironContext() {
        return environContext.get();
    }

    public static void clearEnvironContext() {
        getEnvironContext().clear();
        environContext.remove();
    }

    public static void clearAll() {
        clearUserContext();
        clearEnvironContext();
    }

}
